package com.haiertvbic.tvnow.network.paser;

import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;

import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserException;

import com.haiertvbic.tvnow.model.ProgramItem;

public class FilterItemPaser extends XmlPaser {
	ArrayList<ProgramItem> items = null;

	public FilterItemPaser(InputStream result) {
		this.result = result;
	}

	@Override
	public ArrayList<ProgramItem> getContent() {
		ProgramItem item = null;
		try {
			parser.setInput(result, "UTF-8");
			int eventType = parser.getEventType();
			while (eventType != XmlPullParser.END_DOCUMENT) {
				switch (eventType) {
				case XmlPullParser.START_DOCUMENT:// 文档开始事件,可以进行数据初始化处理
					items = new ArrayList<ProgramItem>();
					break;
				case XmlPullParser.START_TAG:// 开始元素事件
					if ("fitem".equals(parser.getName())) {
						item = new ProgramItem();
					} else if ("epgNode".equals(parser.getName())) {
						String node = parser.nextText();
						item.setNode(node);
					} else if ("epgName".equals(parser.getName())) {
						String epgName = parser.nextText();
						item.setName(epgName);
					} else if ("epgPNode".equals(parser.getName())) {
						String epgPNode = parser.nextText();
						item.setPnode(epgPNode);
					}
					break;
				case XmlPullParser.END_TAG:// 结束元素事件
					if ("fitem".equals(parser.getName()) && item != null) {
						items.add(item);
					}
					break;

				}
				eventType = parser.next();
			}
		} catch (XmlPullParserException e) {
		} catch (Exception e) {
		} finally {
			if (result != null) {
				try {
					result.close();
				} catch (IOException e) {
				}
			}
		}
		return items;
	}

}
